package com.alibaba.algo.apiimpl.app;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.algo.cooperation.common.BaseResult;
import com.alibaba.algo.cooperation.usercomponent.service.app.AppAccountService;
import com.alibaba.algo.cooperation.usercomponent.vo.req.app.CreateAppAccountRequestVO;
import com.alibaba.algo.cooperation.usercomponent.vo.req.app.DeleteAppAccountRequestVO;
import com.alibaba.algo.cooperation.usercomponent.vo.req.app.SelectAppAccountRequestVO;
import com.alibaba.algo.cooperation.usercomponent.vo.req.app.UpdateAppAccountRequestVO;
import com.alibaba.algo.cooperation.usercomponent.vo.res.app.AppAccountResponseVO;
import com.alibaba.algo.core.base.YesOrNo;
import com.alibaba.algo.core.exception.BaseException;
import com.alibaba.algo.domain.app.AppAccountDomain;
import com.alibaba.algo.domain.app.AppFunctionDomain;
import com.alibaba.algo.entity.app.AppAccount;
import com.alibaba.algo.entity.app.AppFunction;
import com.alibaba.algo.exception.BaseErrorCode;

import lombok.extern.slf4j.Slf4j;

/**
 * 类AppAccountServiceImpl.java的实现描述：应用账号 serviceimpl
 *
 * @author nieganggang 2021-03-12 16:08:17
 */
@Slf4j
@RestController
public class AppAccountServiceImpl implements AppAccountService {

    @Autowired
    private AppAccountDomain appAccountDomain;

    @Autowired
    private AppFunctionDomain appFunctionDomain;

    /**
     * 新增应用账号
     *
     * @param vo 入参
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void insert(CreateAppAccountRequestVO vo) {
        appAccountDomain.add();
    }

    /**
     * 删除应用账号
     *
     * @param vo 账号、功能标识
     * @return 删除结果
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public BaseResult delete(DeleteAppAccountRequestVO vo) {
        if (ObjectUtils.isEmpty(vo.getId())) {
            throw new BaseException(BaseErrorCode.APP_ID_IS_NULL.getCode(), BaseErrorCode.APP_ID_IS_NULL.getMessage());
        }
        if (ObjectUtils.isEmpty(vo.getAppFunctionId())) {
            throw new BaseException(BaseErrorCode.APP_FUNCTION_ID_IS_NULL.getCode(), BaseErrorCode.APP_FUNCTION_ID_IS_NULL.getMessage());
        }
        // 删除应用账号表
        AppAccount appAccount = new AppAccount();
        appAccount.setId(vo.getId());
        appAccount.setIsDeleted(YesOrNo.Y.name());
        appAccountDomain.updateById(appAccount);
        // 删除应用功能表
        AppFunction appFunction = new AppFunction();
        appFunction.setId(vo.getAppFunctionId());
        appFunction.setIsDeleted(YesOrNo.Y.name());
        appFunctionDomain.updateById(appFunction);
        return BaseResult.buildSuccess();
    }

    @Override
    public void update(UpdateAppAccountRequestVO vo) {
        AppAccount appAccount = new AppAccount();
        BeanUtils.copyProperties(vo, appAccount);
        appAccountDomain.updateById(appAccount);
    }

    /**
     * 查询应用下的账号列表(全量)
     *
     * @param vo 入参
     * @return 账号列表
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true, rollbackFor = Exception.class)
    public BaseResult<List<AppAccountResponseVO>> selectList(SelectAppAccountRequestVO vo) {
        if (ObjectUtils.isEmpty(vo.getAppId())) {
            throw new BaseException(BaseErrorCode.APP_ID_IS_NULL.getCode(), BaseErrorCode.APP_ID_IS_NULL.getMessage());
        }
        List<AppAccountResponseVO> accountList = appFunctionDomain.selectList(vo.getAppId());
        return BaseResult.buildSuccess(accountList);
    }
}
